<?php

//fetch the passed request
$request = $_SERVER['QUERY_STRING'];
$post = $_POST;
$file = $_FILES;

class Router{
   
   
   public function action ($request){
		//parse the page request and other GET variables
		$parsed = explode('&' , $request);
		//the page is the first element
		$page = array_shift($parsed);
		list($page,$method) = split ("/", $page,2);
		//the rest of the array are get statements, parse them out.
		$getVars = array();
		foreach ($parsed as $argument)
		{
			//split GET vars along '=' symbol to separate variable, values
			list($variable , $value) = split('=' , $argument);
			$getVars[$variable] = urldecode($value);
		}

		//compute the path to the file
		$target = $_SERVER['DOCUMENT_ROOT'] . '/fatec-si/controllers/' . $page . '.php';
		//get target
		if (file_exists($target))
		{
			include_once($target);
			//modify page to fit naming convention
			$class = ucfirst($page) . 'Controller';
	
			//instantiate the appropriate class
			if (class_exists($class))
			{
				$controller = new $class;		
			}
			else
			{
				//did we name our class correctly?
				die('class does not exist!');
			}
		}
		else
		{
			//can't find the file in 'controllers'! 
			die('page does not exist!');
		}

		//once we have the controller instantiated, execute the default function
		//pass any GET varaibles to the main method
		$controller->$method($getVars);
   }
   
};

$router =  new Router();
$router->action($request);

